/*
Copyright (c) 2009, Joseph J. Bergeron
All rights reserved.

Redistribution  and  use  in  source  and  binary  forms,  with  or  without  
modification,  are  permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation 
     and/or other materials provided with the distribution.
    * Neither the name of the creators nor the names of its contributors
      may be used to endorse or promote products derived from this software 
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package hatkins.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	/**
	 * A view with a render buffer
	 */
	public class RenderedHatkinsView extends HatkinsView
	{
		private var _viewport:Bitmap;

		/**
		 * Create the view
		 */
		public function RenderedHatkinsView() {
			super();
			_viewport = new Bitmap();
			addChild(_viewport);
		}
		
		/**
		 * @copy
		 */
		public override function setViewport(viewportX:Number, viewportY:Number, viewportWidth:Number, viewportHeight:Number):void {
			super.setViewport(viewportX, viewportY, viewportWidth, viewportHeight);
			_updateViewport();
		}
		
		private function _updateViewport():void {
			var bitmapData:BitmapData = _viewport.bitmapData;
			if (bitmapData==null || bitmapData.width != viewportWidth || bitmapData.height != viewportHeight) {
				_viewport.bitmapData = new BitmapData(viewportWidth, viewportHeight);
				renderAll();
			}
			_viewport.x = viewportX;
			_viewport.y = viewportY;
		}
		
		/**
		 * Get the render buffer
		 */
		public function get renderBuffer():BitmapData {
			return _viewport.bitmapData;
		}
		
		/**
		 * Clear the render buffer to a color
		 */
		public function clear(color:uint = 0):void {
			clearRect(0, 0, viewportWidth, viewportHeight, color);
		}

		/**
		 * Clear a rectangular portion of the render buffer
		 */
		public function clearRect(rectX:int, rectY:int, rectWidth:int, rectHeight:int, color:uint = 0):void {
			if (renderBuffer!=null) {
				renderBuffer.fillRect(new Rectangle(rectX, rectY, rectWidth, rectHeight), color);
			}
		}
		
		/**
		 * Render a rectangular portion of the buffer
		 */
		public function render(invalidX:int, invalidY:int, invalidWidth:int, invalidHeight:int):void {
			// empty
		}
		
		/**
		 * Render over the whole viewport
		 */
		public function renderAll():void {
			render(0, 0, viewportWidth, viewportHeight);
		}
	}
}